﻿using System;
using System.Collections;
using System.ComponentModel;

namespace Kugar.Core.SL4.Control
{
    public class PagedCollectionView : IPagedCollectionView, INotifyPropertyChanged, IEnumerable
    {
        public PagedCollectionView()
        {
            
        }

        public PagedCollectionView(int pageIndex,int pageSize,int itemCount)
        {
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;
            this.ItemCount = itemCount;
        }

        #region IPagedCollectionView Members

        public bool CanChangePage
        {
            get { return true; }
        }

        private bool _isPageChanging;

        public bool IsPageChanging
        {
            get { return _isPageChanging; }
            private set
            {
                if (_isPageChanging != value)
                {
                    _isPageChanging = value;
                    OnPropertyChanged("IsPageChanging");
                }
            }
        }

        public int ItemCount
        {
            get { return TotalItemCount; }
            set { TotalItemCount = value; }
        }

        public bool MoveToFirstPage()
        {
            return this.MoveToPage(0);
        }

        public bool MoveToLastPage()
        {
            return (((this.TotalItemCount != -1) && (this.PageSize > 0)) && this.MoveToPage(this.PageCount - 1));
        }

        public bool MoveToNextPage()
        {
            return MoveToPage(_pageIndex + 1);
        }

        public bool MoveToPage(int pageIndex)
        {
            if (pageIndex < -1)
            {
                return false;
            }
            if ((pageIndex == -1) && (this.PageSize > 0))
            {
                return false;
            }
            if ((pageIndex >= this.PageCount) || (this._pageIndex == pageIndex))
            {
                return false;
            }

            try
            {
                IsPageChanging = true;
                if (null != PageChanging)
                {
                    PageChangingEventArgs args = new PageChangingEventArgs(pageIndex);
                    OnPageChanging(args);
                    if (args.Cancel) return false;
                }

                _pageIndex = pageIndex;
                //Refresh();
                IsPageChanging = false;
                OnPropertyChanged("PageIndex");
                OnPageChanged(EventArgs.Empty);
                return true;
            }
            finally
            {
                IsPageChanging = false;
            }
        }

        public bool MoveToPreviousPage()
        {
            return MoveToPage(_pageIndex - 1);
        }

        public event EventHandler<EventArgs> PageChanged;

        public event EventHandler<PageChangingEventArgs> PageChanging;

        public int PageCount
        {
            get
            {
                if (this._pageSize <= 0)
                {
                    return 0;
                }
                return Math.Max(1, (int)Math.Ceiling(((double)this.ItemCount) / ((double)this._pageSize)));

            }
        }

        private int _pageIndex;

        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        protected virtual void OnPageChanging(PageChangingEventArgs args)
        {
            if (null != PageChanging)
            {
                PageChanging(this, args);
            }
        }

        protected virtual void OnPageChanged(EventArgs args)
        {
            if (null != PageChanged)
            {
                PageChanged(this, args);
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private int _pageSize = 10;

        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                if (_pageSize != value && value >= 1)
                {
                    _pageSize = value;
                    //OnPropertyChanged("PageSize");
                }
            }
        }

        private int _totalItemCount;

        public int TotalItemCount
        {
            get { return _totalItemCount; }
            set
            {
                if (_totalItemCount != value)
                {
                    _totalItemCount = value;
                    OnPropertyChanged("TotalItemCount");
                    OnPropertyChanged("ItemCount");
                }
            }
        }

        public IEnumerable ItemSource { set; get; }

        #endregion


        public event PropertyChangedEventHandler PropertyChanged;



        public IEnumerator GetEnumerator()
        {
            if (ItemSource == null)
            {
                return null;
            }
            else
            {
                return ItemSource.GetEnumerator();
            }

        }
    }
}
